package viettravel.guiandevent.place;

import java.util.ArrayList;
import org.holoeverywhere.widget.ListView;
import org.json.JSONException;
import org.json.JSONObject;
import viettravel.DAO.LocalMapDAO;
import viettravel.common.Constants;
import viettravel.guiandevent.R;
import viettravel.guiandevent.map.GooglemapV2GetLatLngActivity;
import viettravel.guiandevent.map.ListMoveMapAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class ListCheckPlaceName extends Activity {
	private ArrayList<LocalMapDAO> listLocalMapDAO;
	private ListMoveMapAdapter listLocalMapAdapter;
	private TextView listCheckNamePre;
	private Button listCheckNameChooseOnMap;
	private String mPlaceName;
	private Double mPlaceLat;
	private Double mPlaceLong;
	private static final int RESULT_LOAD_PLACE_FROM_MAP = 5;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.customview_checkname_dialog);
		setTitle(getString(R.string.placedetail_dialogcomment_title));
		ListView listPlaceName = (ListView)findViewById(R.id.listCheckNameList);
		listCheckNamePre = (TextView)findViewById(R.id.listCheckNamePre);
		listCheckNamePre.setTypeface(Constants.TYPEFACE_NYALA);
		listCheckNameChooseOnMap = (Button)findViewById(R.id.listCheckNameChooseOnMap);
		listCheckNameChooseOnMap.setTypeface(Constants.TYPEFACE_NYALA);
		listCheckNameChooseOnMap.setOnClickListener(chooseOnMapOnClickListener);
		
		listLocalMapDAO = new ArrayList<LocalMapDAO>();
		Bundle extra = getIntent().getExtras();
		mPlaceName = extra.getString(Constants.EXTRA_NEWPLACE_CHECKNAME_PLACENAME);
		if (extra.getInt(Constants.EXTRA_NEWPLACE_CHECKNAME_VALUE) == 0)
			listCheckNamePre
					.setText(getString(R.string.newplace_checkname_dialog_prenull) + " " + mPlaceName);
		else {
			listCheckNamePre
					.setText(getString(R.string.newplace_checkname_dialog_pre)
							+ " "
							+ mPlaceName
							+ " :");
			ArrayList<String> temp = extra
					.getStringArrayList(Constants.LOCATIONLIST);
			for (int i = 0; i < temp.size(); i++) {
				try {
					JSONObject jsonObject = new JSONObject(temp.get(i));
					String placeName = jsonObject
							.getJSONArray(Constants.TAG_ADDRESS_COMPONENTS)
							.getJSONObject(0)
							.getString(Constants.TAG_LONG_NAME);
					String placeAddress = jsonObject
							.getJSONArray(Constants.TAG_ADDRESS_COMPONENTS)
							.getJSONObject(1)
							.getString(Constants.TAG_LONG_NAME)
							+ " ,"
							+ jsonObject
									.getJSONArray(
											Constants.TAG_ADDRESS_COMPONENTS)
									.getJSONObject(2)
									.getString(Constants.TAG_LONG_NAME)
							+ " ,"
							+ jsonObject
									.getJSONArray(
											Constants.TAG_ADDRESS_COMPONENTS)
									.getJSONObject(3)
									.getString(Constants.TAG_LONG_NAME);
					Double placeLat = jsonObject
							.getJSONObject(Constants.TAG_GEOMETRY)
							.getJSONObject(Constants.TAG_LOCATION)
							.getDouble(Constants.TAG_LAT);
					Double placeLng = jsonObject
							.getJSONObject(Constants.TAG_GEOMETRY)
							.getJSONObject(Constants.TAG_LOCATION)
							.getDouble(Constants.TAG_LNG);
					Log.v(Constants.TAG + " result" + i, placeName);
					Log.v(Constants.TAG + " result" + i, placeAddress);
					LocalMapDAO l = new LocalMapDAO(getResources().getDrawable(
							R.drawable.app_icon), placeName, placeAddress,
							placeLat, placeLng);
					listLocalMapDAO.add(l);
				} catch (JSONException e) {
					e.printStackTrace();
					Log.e(Constants.TAG, "Loi parse json ListCheckPlaceName");
				}
			}
		}
		listLocalMapAdapter = new ListMoveMapAdapter(this, listLocalMapDAO);
		listPlaceName.setAdapter(listLocalMapAdapter);
		listPlaceName.setOnItemClickListener(listViewOnItemClickListener);		
	}
	
	OnItemClickListener listViewOnItemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int itemId,
				long arg3) {
			
			LocalMapDAO localMapDAO = listLocalMapDAO.get(itemId);
			Intent i = new Intent();
			mPlaceName = localMapDAO.getName();
			mPlaceLat =localMapDAO.getLatitude();
			mPlaceLong = localMapDAO.getLongtitude();
			i.putExtra(Constants.EXTRA_PLACE_NAME, mPlaceName );
			i.putExtra(Constants.EXTRA_PLACE_LONG, mPlaceLong);
			i.putExtra(Constants.EXTRA_PLACE_LAT, mPlaceLat);
			setResult(RESULT_OK, i);
			finish();
		}
	};
	
	View.OnClickListener chooseOnMapOnClickListener = new OnClickListener() {		
		@Override
		public void onClick(View v) {
			Intent intent = new Intent(ListCheckPlaceName.this, GooglemapV2GetLatLngActivity.class);
			intent.putExtra(Constants.EXTRA_NEWPLACE_CHECKNAME_PLACENAME, mPlaceName);
			startActivityForResult(intent, RESULT_LOAD_PLACE_FROM_MAP);
		}
	};
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Log.v(Constants.TAG, requestCode + "");
		if (requestCode == RESULT_LOAD_PLACE_FROM_MAP && resultCode == RESULT_OK && null != data) {
			mPlaceName = data.getStringExtra(Constants.EXTRA_PLACE_NAME);
			mPlaceLong = data.getDoubleExtra(Constants.EXTRA_PLACE_LONG, 0);
			mPlaceLat = data.getDoubleExtra(Constants.EXTRA_PLACE_LAT, 0);
			
			Intent i = new Intent();
			i.putExtra(Constants.EXTRA_PLACE_NAME, mPlaceName);
			i.putExtra(Constants.EXTRA_PLACE_LONG, mPlaceLong);
			i.putExtra(Constants.EXTRA_PLACE_LAT, mPlaceLat);
			setResult(RESULT_OK, i);
			finish();
		}
	}

}
